﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Web;

namespace G.Service.Service
{
    /// <summary>
    /// GPSConvertService 的摘要说明
    /// </summary>
    public class GPSConvertService : BaseHandler
    {

        protected override void ProcessRequest(HttpContext context, out BLL.IResult result, string action)
        {
            switch (action)
            {
                case "wordtobaidu":
                    double x=double.Parse(QueryString["x"]);
                    double y=double.Parse(QueryString["y"]);
                    result = new BLL.Result<Point> { IsOK = true, ResponseData = GPSConvert.WordToBaidu(new Point(x, y)) };
                    break;
                default:
                    result = new BLL.Result() { Msg = "指定的action无效！" };
                    break;
            }
        }
    }

    [DataContract]
    public class Point
    {
        public Point(double x, double y)
        {
            this.Latitude = x;
            this.Longitude = y;
        }
        [DataMember]
        public double Latitude { get; set; }
        [DataMember]
        public double Longitude { get; set; }
    }
    public static class GPSConvert
    {
        /// <summary>
        /// 地球坐标转百度坐标
        /// </summary>
        /// <param name="wg"></param>
        /// <returns></returns>
        public static Point WordToBaidu(Point wg)
        {
            var mg = Transform(wg);
            return MarsToBaidu(mg);
        }
        #region 中国坐标和百度坐标互转
        /// <summary>
        /// 中国坐标转换成百度坐标
        /// </summary>
        /// <param name="mg">中国坐标</param>
        /// <returns>百度坐标</returns>
        public static Point MarsToBaidu(Point mg)
        {
            double x = mg.Longitude, y = mg.Latitude;

            double z = Math.Sqrt(x * x + y * y) + 0.00002 * Math.Sin(y * x_pi);

            double theta = Math.Atan2(y, x) + 0.000003 * Math.Cos(x * x_pi);

            double bd_lon = z * Math.Cos(theta) + 0.0065;

            double bd_lat = z * Math.Sin(theta) + 0.006;

            return new Point(bd_lat, bd_lon);
        }
        const double x_pi = 3.14159265358979324 * 3000.0 / 180.0;
        /// <summary>
        /// 百度坐标转换成火星坐标
        /// </summary>
        /// <param name="bdg"></param>
        /// <returns></returns>
        public static Point BaiduToMars(Point bdg)
        {
            double x = bdg.Longitude - 0.0065, y = bdg.Latitude - 0.006;

            double z = Math.Sqrt(x * x + y * y) - 0.00002 * Math.Sin(y * x_pi);

            double theta = Math.Atan2(y, x) - 0.000003 * Math.Cos(x * x_pi);

            double gg_lon = z * Math.Cos(theta);

            double gg_lat = z * Math.Sin(theta);
            return new Point(gg_lat, gg_lon);
        }
        #endregion

        #region 地球坐标转中国坐标
        const double pi = 3.14159265358979324;
        const double a = 6378245.0;
        const double ee = 0.00669342162296594323;

        /// <summary>
        /// 地球坐标转换成火星坐标（中国坐标）
        /// </summary>
        /// <param name="wg">地球坐标</param>
        /// <returns>中国坐标</returns>
        public static Point Transform(Point wg)
        {
            double lat;
            double lng;
            Transform(wg.Latitude, wg.Longitude, out lat, out lng);
            return new Point(lat, lng);
        }

        /// <summary>
        /// 地球坐标转换成火星坐标（中国坐标）
        /// </summary>
        /// <param name="wgLat"></param>
        /// <param name="wgLon"></param>
        /// <param name="mgLat"></param>
        /// <param name="mgLon"></param>
        public static void Transform(double wgLat, double wgLon, out double mgLat, out double mgLon)
        {
            if (outOfChina(wgLat, wgLon))
            {
                mgLat = wgLat;
                mgLon = wgLon;
                return;
            }
            double dLat = transformLat(wgLon - 105.0, wgLat - 35.0);
            double dLon = transformLon(wgLon - 105.0, wgLat - 35.0);
            double radLat = wgLat / 180.0 * pi;
            double magic = Math.Sin(radLat);
            magic = 1 - ee * magic * magic;
            double sqrtMagic = Math.Sqrt(magic);
            dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
            dLon = (dLon * 180.0) / (a / sqrtMagic * Math.Cos(radLat) * pi);
            mgLat = wgLat + dLat;
            mgLon = wgLon + dLon;
        }

        static bool outOfChina(double lat, double lon)
        {
            if (lon < 72.004 || lon > 137.8347)
                return true;
            if (lat < 0.8293 || lat > 55.8271)
                return true;
            return false;
        }

        static double transformLat(double x, double y)
        {
            double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.Sqrt(Math.Abs(x));
            ret += (20.0 * Math.Sin(6.0 * x * pi) + 20.0 * Math.Sin(2.0 * x * pi)) * 2.0 / 3.0;
            ret += (20.0 * Math.Sin(y * pi) + 40.0 * Math.Sin(y / 3.0 * pi)) * 2.0 / 3.0;
            ret += (160.0 * Math.Sin(y / 12.0 * pi) + 320 * Math.Sin(y * pi / 30.0)) * 2.0 / 3.0;
            return ret;
        }

        static double transformLon(double x, double y)
        {
            double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.Sqrt(Math.Abs(x));
            ret += (20.0 * Math.Sin(6.0 * x * pi) + 20.0 * Math.Sin(2.0 * x * pi)) * 2.0 / 3.0;
            ret += (20.0 * Math.Sin(x * pi) + 40.0 * Math.Sin(x / 3.0 * pi)) * 2.0 / 3.0;
            ret += (150.0 * Math.Sin(x / 12.0 * pi) + 300.0 * Math.Sin(x / 30.0 * pi)) * 2.0 / 3.0;
            return ret;
        }

        #endregion
    }
}